﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using Maticsoft.DBUtility;//Please add references
namespace com.qbengo.utility.nodepicker.DAL
{
    /// <summary>
    /// 数据访问类:EventDao
    /// </summary>
    public partial class EventDao
    {
        public EventDao()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int eventid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from event");
            strSql.Append(" where eventid=@eventid");
            MySqlParameter[] parameters = {
					new MySqlParameter("@eventid", MySqlDbType.Int32)
			};
            parameters[0].Value = eventid;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(com.qbengo.utility.nodepicker.entity.Event model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into event(");
            strSql.Append("venueid,event_type_id,event_category_id,name,`desc`,utc_from,utc_to,hours,pricing,url_info,url_link,image_url,organizer,event_poi)");
            strSql.Append(" values (");
            strSql.Append("@venueid,@event_type_id,@event_category_id,@name,@desc,@utc_from,@utc_to,@hours,@pricing,@url_info,@url_link,@image_url,@organizer,@event_poi)");
            MySqlParameter[] parameters = {
					new MySqlParameter("@venueid", MySqlDbType.Int32,11),
					new MySqlParameter("@event_type_id", MySqlDbType.Int32,11),
					new MySqlParameter("@event_category_id", MySqlDbType.Int32,11),
					new MySqlParameter("@name", MySqlDbType.Text),
					new MySqlParameter("@desc", MySqlDbType.Text),
					new MySqlParameter("@utc_from", MySqlDbType.DateTime),
					new MySqlParameter("@utc_to", MySqlDbType.DateTime),
					new MySqlParameter("@hours", MySqlDbType.Text),
					new MySqlParameter("@pricing", MySqlDbType.Text),
					new MySqlParameter("@url_info", MySqlDbType.Text),
					new MySqlParameter("@url_link", MySqlDbType.Text),
					new MySqlParameter("@image_url", MySqlDbType.Text),
					new MySqlParameter("@organizer", MySqlDbType.Text),
					new MySqlParameter("@event_poi", MySqlDbType.Int32,11)};
            parameters[0].Value = model.venueid;
            parameters[1].Value = model.event_type_id;
            parameters[2].Value = model.event_category_id;
            parameters[3].Value = model.name;
            parameters[4].Value = model.desc;
            parameters[5].Value = model.utc_from;
            parameters[6].Value = model.utc_to;
            parameters[7].Value = model.hours;
            parameters[8].Value = model.pricing;
            parameters[9].Value = model.url_info;
            parameters[10].Value = model.url_link;
            parameters[11].Value = model.image_url;
            parameters[12].Value = model.organizer;
            parameters[13].Value = model.event_poi;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(com.qbengo.utility.nodepicker.entity.Event model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update event set ");
            strSql.Append("venueid=@venueid,");
            strSql.Append("event_type_id=@event_type_id,");
            strSql.Append("event_category_id=@event_category_id,");
            strSql.Append("name=@name,");
            strSql.Append("desc=@desc,");
            strSql.Append("utc_from=@utc_from,");
            strSql.Append("utc_to=@utc_to,");
            strSql.Append("hours=@hours,");
            strSql.Append("pricing=@pricing,");
            strSql.Append("url_info=@url_info,");
            strSql.Append("url_link=@url_link,");
            strSql.Append("image_url=@image_url,");
            strSql.Append("organizer=@organizer,");
            strSql.Append("event_poi=@event_poi");
            strSql.Append(" where eventid=@eventid");
            MySqlParameter[] parameters = {
					new MySqlParameter("@venueid", MySqlDbType.Int32,11),
					new MySqlParameter("@event_type_id", MySqlDbType.Int32,11),
					new MySqlParameter("@event_category_id", MySqlDbType.Int32,11),
					new MySqlParameter("@name", MySqlDbType.Text),
					new MySqlParameter("@desc", MySqlDbType.Text),
					new MySqlParameter("@utc_from", MySqlDbType.DateTime),
					new MySqlParameter("@utc_to", MySqlDbType.DateTime),
					new MySqlParameter("@hours", MySqlDbType.Text),
					new MySqlParameter("@pricing", MySqlDbType.Text),
					new MySqlParameter("@url_info", MySqlDbType.Text),
					new MySqlParameter("@url_link", MySqlDbType.Text),
					new MySqlParameter("@image_url", MySqlDbType.Text),
					new MySqlParameter("@organizer", MySqlDbType.Text),
					new MySqlParameter("@event_poi", MySqlDbType.Int32,11),
					new MySqlParameter("@eventid", MySqlDbType.Int32,11)};
            parameters[0].Value = model.venueid;
            parameters[1].Value = model.event_type_id;
            parameters[2].Value = model.event_category_id;
            parameters[3].Value = model.name;
            parameters[4].Value = model.desc;
            parameters[5].Value = model.utc_from;
            parameters[6].Value = model.utc_to;
            parameters[7].Value = model.hours;
            parameters[8].Value = model.pricing;
            parameters[9].Value = model.url_info;
            parameters[10].Value = model.url_link;
            parameters[11].Value = model.image_url;
            parameters[12].Value = model.organizer;
            parameters[13].Value = model.event_poi;
            parameters[14].Value = model.eventid;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int eventid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from event ");
            strSql.Append(" where eventid=@eventid");
            MySqlParameter[] parameters = {
					new MySqlParameter("@eventid", MySqlDbType.Int32)
			};
            parameters[0].Value = eventid;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string eventidlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from event ");
            strSql.Append(" where eventid in (" + eventidlist + ")  ");
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public com.qbengo.utility.nodepicker.entity.Event GetModel(int eventid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select eventid,venueid,event_type_id,event_category_id,name,`desc`,utc_from,utc_to,hours,pricing,url_info,url_link,image_url,organizer,event_poi from event ");
            strSql.Append(" where eventid=@eventid");
            MySqlParameter[] parameters = {
					new MySqlParameter("@eventid", MySqlDbType.Int32)
			};
            parameters[0].Value = eventid;

            com.qbengo.utility.nodepicker.entity.Event model = new com.qbengo.utility.nodepicker.entity.Event();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["eventid"] != null && ds.Tables[0].Rows[0]["eventid"].ToString() != "")
                {
                    model.eventid = int.Parse(ds.Tables[0].Rows[0]["eventid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["venueid"] != null && ds.Tables[0].Rows[0]["venueid"].ToString() != "")
                {
                    model.venueid = int.Parse(ds.Tables[0].Rows[0]["venueid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["event_type_id"] != null && ds.Tables[0].Rows[0]["event_type_id"].ToString() != "")
                {
                    model.event_type_id = int.Parse(ds.Tables[0].Rows[0]["event_type_id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["event_category_id"] != null && ds.Tables[0].Rows[0]["event_category_id"].ToString() != "")
                {
                    model.event_category_id = int.Parse(ds.Tables[0].Rows[0]["event_category_id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["name"] != null && ds.Tables[0].Rows[0]["name"].ToString() != "")
                {
                    model.name = ds.Tables[0].Rows[0]["name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["desc"] != null && ds.Tables[0].Rows[0]["desc"].ToString() != "")
                {
                    model.desc = ds.Tables[0].Rows[0]["desc"].ToString();
                }
                if (ds.Tables[0].Rows[0]["utc_from"] != null && ds.Tables[0].Rows[0]["utc_from"].ToString() != "")
                {
                    model.utc_from = DateTime.Parse(ds.Tables[0].Rows[0]["utc_from"].ToString());
                }
                if (ds.Tables[0].Rows[0]["utc_to"] != null && ds.Tables[0].Rows[0]["utc_to"].ToString() != "")
                {
                    model.utc_to = DateTime.Parse(ds.Tables[0].Rows[0]["utc_to"].ToString());
                }
                if (ds.Tables[0].Rows[0]["hours"] != null && ds.Tables[0].Rows[0]["hours"].ToString() != "")
                {
                    model.hours = ds.Tables[0].Rows[0]["hours"].ToString();
                }
                if (ds.Tables[0].Rows[0]["pricing"] != null && ds.Tables[0].Rows[0]["pricing"].ToString() != "")
                {
                    model.pricing = ds.Tables[0].Rows[0]["pricing"].ToString();
                }
                if (ds.Tables[0].Rows[0]["url_info"] != null && ds.Tables[0].Rows[0]["url_info"].ToString() != "")
                {
                    model.url_info = ds.Tables[0].Rows[0]["url_info"].ToString();
                }
                if (ds.Tables[0].Rows[0]["url_link"] != null && ds.Tables[0].Rows[0]["url_link"].ToString() != "")
                {
                    model.url_link = ds.Tables[0].Rows[0]["url_link"].ToString();
                }
                if (ds.Tables[0].Rows[0]["image_url"] != null && ds.Tables[0].Rows[0]["image_url"].ToString() != "")
                {
                    model.image_url = ds.Tables[0].Rows[0]["image_url"].ToString();
                }
                if (ds.Tables[0].Rows[0]["organizer"] != null && ds.Tables[0].Rows[0]["organizer"].ToString() != "")
                {
                    model.organizer = ds.Tables[0].Rows[0]["organizer"].ToString();
                }
                if (ds.Tables[0].Rows[0]["event_poi"] != null && ds.Tables[0].Rows[0]["event_poi"].ToString() != "")
                {
                    model.event_poi = int.Parse(ds.Tables[0].Rows[0]["event_poi"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select eventid,venueid,event_type_id,event_category_id,name,`desc`,utc_from,utc_to,hours,pricing,url_info,url_link,image_url,organizer,event_poi ");
            strSql.Append(" FROM event ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM event ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.eventid desc");
            }
            strSql.Append(")AS Row, T.*  from event T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            MySqlParameter[] parameters = {
                    new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
                    new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
                    new MySqlParameter("@PageSize", MySqlDbType.Int32),
                    new MySqlParameter("@PageIndex", MySqlDbType.Int32),
                    new MySqlParameter("@IsReCount", MySqlDbType.Bit),
                    new MySqlParameter("@OrderType", MySqlDbType.Bit),
                    new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "event";
            parameters[1].Value = "eventid";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

